//
//  LoginViewController.m
//  GTTool
//
//  Created by GTO on 2018/8/27.
//  Copyright © 2018年 GTO. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginViewModel.h"
@interface LoginViewController ()
@property (nonatomic, strong) LoginViewModel *loginViewModel;
@property (nonatomic, strong) UITextField *accountField;
@property (nonatomic, strong) UITextField *passWordField;
@property (nonatomic, strong) UIButton *loginButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self bindModel];
}
#pragma mark - 创建UI
- (void)initUI{
    UITextField *accountField = [[UITextField alloc] init];
    self.accountField = accountField;
    [self.view addSubview:accountField];
    
    UITextField *passWordField = [[UITextField alloc] init];
    self.passWordField = passWordField;
    [self.view addSubview:passWordField];
    
    UIButton *loginButton = [[UIButton alloc] init];
    self.loginButton = loginButton;
    [self.view addSubview:loginButton];
     @weakify(self);
    [[loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
     @strongify(self)
        //执行登录
        [self.loginViewModel.LoginCommand execute:nil];
    }];
 
   
}
#pragma mark - 数据绑定
- (void)bindModel{
    RAC(self.loginViewModel,account) = self.accountField.rac_textSignal;
    RAC(self.loginViewModel,passWord) = self.passWordField.rac_textSignal;
    _loginButton.enabled = [RACSignal combineLatest:@[RACObserve(self.loginViewModel, account),RACObserve(self.loginViewModel, passWord)] reduce:^id (NSString *account,NSString *passWord){
        return @(account.length>0 && passWord.length>0);
    }];
    //接受登录成功的数据
    @weakify(self);
    [self.loginViewModel.LoginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        
    }];
}
#pragma mark - getter方法
-(LoginViewModel *)loginViewModel{
    if(_loginViewModel == nil){
        _loginViewModel = [[LoginViewModel alloc] init];
    }
    return  _loginViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
